package com.apes.project.jekunAuto.timeTask.scm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.pi.service.PiManager;
import com.apes.scm.masterdata.stock.model.Location;
import com.apes.scm.masterdata.stock.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.project.jekunAuto.timeTask.scm
 * @ClassName: LocationSynService
 * @Author: pyb
 * @Description: 仓库同步接口
 * @CreateTime: 2021-02-26 11:57
 * @Version: 1.0
 */
@Service("locationSynService")
public class LocationSynService {
    PiManager piManager;

    @Autowired
    void setPiManager(PiManager piManager) {
        this.piManager = piManager;
    }

    @Autowired
    LocationRepository locationRepository;

    public JSONArray synLocation(SimpleRequest request) {
        JSONArray objects = piManager.timedTaskSynErpByConditions("mdmDdxxService.getDDXXListByGSID", "DDID");
        return objects;
    }

    /**
     * 同步老系统地点信息的封库标记
     * @param request
     */
    public void saveLocation(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        String locationId = requestJO.getString("CKKQID");
        boolean valid =  !requestJO.getBooleanValue("FCKBJ");

        Location location = locationRepository.findOne(locationId);
        Optional.of(location).ifPresent(value->{
            if (valid != value.isValid()) {
                value.setValid(valid);
                locationRepository.saveAndFlush(value);
            }
        });
    }
}
